package org.looop.studio.noots.analyzer;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;

public class PositionalStopFilter extends TokenFilter {
	private Set<String> stopWords = new HashSet<String>();
	public PositionalStopFilter(TokenStream in, Set<String> stopWords){
		super(in);
		this.stopWords = stopWords;
	}
	
	@Override
	public Token next() throws IOException {
		int increment = 0;
		for(Token token = input.next(); token != null; token = input.next()){
			if(!stopWords.contains(token.termText())){
				token.setPositionIncrement(token .getPositionIncrement() + increment);
				return token;
			}
			increment++;
		}
		return null;
	}
	
}
